/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing;

import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.Ownership;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.swing.workers.OwnershipListSwingWorker;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author stefan
 */
public class OwnershipsTableModel extends AbstractTableModel {
    
    private List<Ownership> ownershipList = new ArrayList<Ownership>();
    private boolean estateView = true;
    private Locale localization = Locale.getDefault();
    
    public OwnershipsTableModel(boolean estateView) {
        this.estateView = estateView;
    }

    @Override
    public int getRowCount() {
        return this.ownershipList.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Ownership ownership = this.ownershipList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return estateView ? ownership.getRealEstate().getAddress() : ownership.getOwner().getFirstName() + " " + ownership.getOwner().getLastName();
            case 1:
                return ownership.getInvestment();
            case 2:
                return new SimpleDateFormat(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("DD.MM.YYYY")).format(ownership.getDateCreated());
            default:
                throw new IllegalStateException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("COLUMNINDEX"));
        }
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return estateView ? java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("ADDRESS") : java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("NAME");
            case 1:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("INVESTMENT");
            case 2:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("DATE FROM");
            default:
                throw new IllegalStateException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnershipsTableModel", localization).getString("COLUMNINDEX"));
        }
    }
    
    public void addOwnership(Ownership ownership) {
        this.ownershipList.add(ownership);
        int lastRow = ownershipList.size()-1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void tryRemoveOwnership(Ownership ownership) {
        int rownum = ownershipList.indexOf(ownership);
        if(-1 != rownum) {
            this.ownershipList.remove(rownum);
            fireTableRowsDeleted(rownum, rownum);
        }
    }
    
    public void addAllOwnerships(Set<Ownership> ownerships) {
        this.ownershipList.addAll(ownerships);
        fireTableDataChanged();
    }
    
    public void clearAllOwnerships() {
        this.ownershipList.clear();
        fireTableDataChanged();
    }
    
    public void updateOwnership(Ownership ownership) {
        this.ownershipList.set(ownershipList.indexOf(ownership), ownership);
        fireTableRowsUpdated(ownershipList.indexOf(ownership), ownershipList.indexOf(ownership));
    }
    
    public Ownership getOwnershipAt(int rownum) {
        return this.ownershipList.get(rownum);
    }
}
